package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsPageMapper;
import cn.wolfcode.trip.base.mapper.NumBrowseMapper;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.NewsPageQueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 日报service的实现类
 */
@Service
public class NewsPageServiceImpl implements INewsPageService {
    @Autowired
    private NewsPageMapper newsPageMapper;
    @Autowired
    private NewsContentMapper contentMapper;
    @Autowired
    private NumBrowseMapper numBrowseMapper;

    /**
     * 郭棋文
     * 更新或保存日报
     * @param news
     */
    @Override
    public void saveOrUpdate(NewsPage news) {

        NewsContent content = news.getNewsContent();
        if (news.getId() == null) {
            newsPageMapper.insert(news);
            if (content != null) {
                content.setId(news.getId());
                contentMapper.insert(content);
            }
        } else {

            if (content != null) {
                content.setId(news.getId());
                contentMapper.updateByPrimaryKey(content);
            }
            newsPageMapper.updateByPrimaryKey(news);

        }


    }

    /**
     * 郭棋文
     * 条件查询
     * @param qo
     * @return
     */

    @Override
    public PageInfo query(NewsPageQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<NewsPage> list = newsPageMapper.selectForList(qo);
        return new PageInfo(list);
    }

    /**
     * 通过关键字查询日报
     * @param qo
     * @return
     */
    public PageInfo searchQuery(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<NewsPage> list = newsPageMapper.selectByKeyword(qo);
        return new PageInfo(list);
    }

    /**
     * 根据关键字查询该日报的总数
     * @param keyword
     * @return
     */
    public int selectCountByKeyword(String keyword) {
        return newsPageMapper.selectCountByKeyword(keyword);
    }

    /**
     * 查询用户收藏的日报
     * @param qo
     * @return
     */
    public PageInfo selectForCollect(CollectQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<NewsPage> list = newsPageMapper.selectForCollect(qo);
        return new PageInfo(list);
    }

    /**
     * 通过news的id查询该条news的文本内容
     * 郭棋文
     * @param newsId
     * @return
     */
    @Override
    public NewsContent selectNewContentByNewsId(Long newsId) {
        return contentMapper.selectByPrimaryKey(newsId);
    }


    /**
     * 通过news的id查询该条news的文本内容
     * 郭棋文
     * @param newsId
     * @return
     */
    @Override
    public NewsPage selectNewsContentById(Long newsId) {
        return newsPageMapper.selectByPrimaryKey(newsId);
    }


    /**
     * 查询所有日报
     * @param qo
     * @return
     */
    @Override
    public PageInfo queryApp(NewsPageQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<NewsPage> list = newsPageMapper.selectForListApp(qo);
        return new PageInfo(list);
    }

    /**
     * 保存浏览量
     * @param nums
     * @return
     */
    public int save(NumBrowse nums) {
        if (numBrowseMapper.selectByPrimaryKey(nums.getId())!=nums) {
            NumBrowse numBrowse = numBrowseMapper.selectByPrimaryKey(nums.getId());
            int num = numBrowse.getNum();
            num++;
            numBrowse.setNum(num);
            numBrowseMapper.updateByPrimaryKey(numBrowse);
            return num;
        }else {
            nums.setNum(1);
            numBrowseMapper.insert(nums);
            return 1;
        }
    }

    /**
     * 查看浏览量
     * @param id
     * @return
     */
    public int selectScanNumber(Long id) {
        NumBrowse nums = new NumBrowse();
        if (numBrowseMapper.selectByPrimaryKey(id)!=null){
            return numBrowseMapper.selectByPrimaryKey(id).getNum();
        } else {
            nums.setId(id);
            nums.setNum(1);
            numBrowseMapper.insert(nums);
            return 1;
        }
    }

}
